<?php


namespace app\model;

use think\Model;
use think\facade\Db;
class ReleaseScopeModel extends Model
{
    protected $table = "store_release_scope";

    /**
     * @param $param
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 查找匹配
     * @author: dongdong
     * @date: 2025/1/7 11:59
     */
    public function search_list($param){
        $type = $param['type']??'0';
        $country = $param['country']??'';
        $identity_type = $param['identity_type']??'';
        $user_id = $param['user_id']??0;
        if(($type=='0')||($type=='')){
            $where = "type = 1";
            if($country!==''){
                $where .= " and operate_country='$country'";
            }
            if($identity_type!==''){
                $where .= " and operate_identity_type='$identity_type'";
            }
            if($user_id){
                $where .= " and user_id='$user_id'";
            }
            $list = self::where($where)->field("operate_id member_id,operate_identity_type identity_type,operate_country country")->select();
            foreach ($list as $k =>&$v){
                $v['member'] = $this->user_info($v['member_id']);
            }
            return $list;
        }elseif ($type=='1'){
            $where = "type = 1";
            if($country!==''){
                $where .= " and user_country='$country'";
            }
            if($identity_type!==''){
                $where .= " and user_identity_type='$identity_type'";
            }
            if($user_id){
                $where .= " and operate_id='$user_id'";
            }
            $list = self::where($where)->field("user_id member_id,user_identity_type identity_type,user_country country")->select();
            foreach ($list as $k =>&$v){
                $v['member'] = $this->user_info($v['member_id']);
            }
            return $list;
        }else{
            $where = "type = 2";
            if($country!==''){
                $where .= " and operate_country='$country'";
            }
            if($identity_type!==''){
                $where .= " and operate_identity_type='$identity_type'";
            }
            if($user_id){
                $where .= " and user_id='$user_id'";
            }
            $list = self::where($where)->field("operate_id member_id,operate_identity_type identity_type,operate_country country")->select();
            foreach ($list as $k =>&$v){
                $v['member'] = $this->user_info($v['member_id']);
            }
            return $list;
        }
    }


    /**
     * @param $user_id
     * @return array|mixed|\think\db\BaseQuery|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 用户信息
     * @author: dongdong
     * @date: 2025/1/7 13:50
     */
    public function user_info($user_id){
        return Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru,mobile,level,avatar")->where(["id"=>$user_id])->find();
    }


}